﻿using UnityEngine;

using System;
using UnityEngine.UI;
using System.Collections.Generic;


public class ResultsManager : MonoBehaviour
{

    private static ResultsManager instance;
    internal static ResultsManager Instance { get { return instance; } }

    private GameObject resultsMenu;

    private bool cleaned, loading, canLoad;
    private float startTime;
    private BarChart barchart;
    private string[] objects;
    private Text resultsText;
    public float maxReachX, maxReachY, maxReachZ;
    public float ArmLength;
    public Vector3 ReachPercentages;
    public float TotalReachVolume;
    public int GestureCount;
    private float gesturesPerMinute;
    public bool On;


    void Awake()
    {

        instance = this;

        resultsMenu = GameObject.Find("ResultsWindow");
        barchart = GameObject.Find("BarChart").GetComponent<BarChart>();
        

        loading = false;
        cleaned = false;
       

    }

    void Start()
    {
        ShowResults();
    }
    internal void ShowResults()
    {

             
        loading = true;
        startTime = Time.realtimeSinceStartup;
       
        Invoke("EnableReachBarChart", 2);
        On = true;


    }
    private void CalculateReachVolume()
    {
        ReachPercentages = new Vector3(maxReachX, maxReachY, maxReachZ);
        ReachPercentages = ReachPercentages / ArmLength;
        TotalReachVolume = (ReachPercentages.x + ReachPercentages.y + ReachPercentages.z) / 3;
    }


    internal void EnableReachBarChart()
    {

       
        barchart.Max = 100;
        barchart.Min = 0;
        barchart.Categories = 3;
        barchart.CreateBars();
        barchart.Bars[0].GetComponent<Bar>().Title = "First Day";
        barchart.Bars[1].GetComponent<Bar>().Title = "Last Lap";
        barchart.Bars[2].GetComponent<Bar>().Title = "This Lap";


        barchart.Bars[0].GetComponent<Bar>().value = 60;
        barchart.Bars[1].GetComponent<Bar>().value = 70;
        barchart.Bars[2].GetComponent<Bar>().value = 20;
        barchart.Bars[0].GetComponent<Bar>().UnitsSymbol = "%";
        barchart.Bars[1].GetComponent<Bar>().UnitsSymbol = "%";
        barchart.Bars[2].GetComponent<Bar>().UnitsSymbol = "%";



        barchart.AnimateBars();
        Invoke("EnableReachPerMinuteBarChart", 25);

    }

    internal void EnableReachPerMinuteBarChart()
    {


        barchart.Reset();
        barchart.Max = 200;
        barchart.Min = 0;
        barchart.Categories = 3;
        barchart.CreateBars();
        barchart.Bars[0].GetComponent<Bar>().Title = "First Day";
        barchart.Bars[1].GetComponent<Bar>().Title = "Last Lap";
        barchart.Bars[2].GetComponent<Bar>().Title = "This Lap";


        barchart.Bars[0].GetComponent<Bar>().value = 72;
        barchart.Bars[1].GetComponent<Bar>().value = 20;
        barchart.Bars[2].GetComponent<Bar>().value = 15;
        barchart.Bars[0].GetComponent<Bar>().UnitsSymbol = "gpm";
        barchart.Bars[1].GetComponent<Bar>().UnitsSymbol = "gpm";
        barchart.Bars[2].GetComponent<Bar>().UnitsSymbol = "gpm";



        barchart.AnimateBars();
        Invoke("EnableScoreBarChart", 25);
    }

    internal void EnableScoreBarChart()
    {

       
        barchart.Reset();
        barchart.Max = 500;
        barchart.Min = 0;
        barchart.Categories = 3;
        barchart.CreateBars();
        barchart.Bars[0].GetComponent<Bar>().Title = "First Day";
        barchart.Bars[1].GetComponent<Bar>().Title = "Last Lap";
        barchart.Bars[2].GetComponent<Bar>().Title = "This Lap";





        barchart.Bars[0].GetComponent<Bar>().value = 100;
        barchart.Bars[1].GetComponent<Bar>().value = 325;
        barchart.Bars[2].GetComponent<Bar>().value = 260;



        barchart.AnimateBars();
    }

    internal void DisableResults()
    {

        ResetResults();

    }

    internal void ResetResults()
    {
        barchart.Reset();


    }

    void Update()
    {
            if (Time.realtimeSinceStartup > startTime + 95)
            {
                DisableResults();
            }
        
    }
}
